package com.material.operation.controllers;

import com.material.operation.aos.MaterialsAo;
import com.material.operation.bean.CastingDistinguish;
import com.material.operation.bean.CastingInformation;
import com.material.operation.bean.GeneralPara;
import com.material.operation.services.CastingDistinguishService;
import com.material.operation.services.CastingInformationService;
import com.material.operation.services.GeneralParaService;
import com.material.operation.services.MaterialsService;
import com.material.operation.vos.MaterailInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author : hongwei.zhang
 * @className : operation - MaterialsController
 * @description : 材料控制器
 * @date : 2020-11-16 20:58
 */
@Controller
@RequestMapping("materials")
public class MaterialsController {
    @Autowired
    private MaterialsService materialsService;

    @Autowired
    private GeneralParaService generalParaService;

    @Autowired
    private CastingDistinguishService castingDistinguishService;

    @Autowired
    private CastingInformationService castingInformationService;

    @RequestMapping("/materail/query")
    public String materailQuery(MaterialsAo materialsAo, Model model){
        //查询集合
        List<String> byRange = materialsService.findByRange(materialsAo);
        model.addAttribute("ranges",byRange);
        return "result_list";
    }

    @RequestMapping("findCastingDistinguishByCodeAndCastingMethod")
    @ResponseBody
    public List<CastingDistinguish> findCastingDistinguishByCodeAndCastingMethod(String code,String castingMethod){
        List<CastingDistinguish> list = castingDistinguishService.findHeatTreatmentListByCodeAndCastingMethod(code, castingMethod);
        return list;
    }


    @RequestMapping("/mechanical/query")
    public String mechanicalQuery(String mechanicalName, Model model){
        CastingDistinguish cd = materialsService.findByName(mechanicalName);
        MaterailInfoVo materailInfoVo = new MaterailInfoVo();
        if(cd != null){
            String code = cd.getCode();
            GeneralPara byCode = generalParaService.findByCode(code);
            if(byCode != null){
                materailInfoVo.setTl(byCode.getTl());
                materailInfoVo.setTs(byCode.getTs());
                materailInfoVo.setTp(byCode.getTp());
            }else{
                materailInfoVo.setTl("0");
                materailInfoVo.setTs("0");
                materailInfoVo.setTp("0");
            }
            materailInfoVo.setCode(cd.getCode());
            materailInfoVo.setMark(cd.getMark());
            materailInfoVo.setElongation_percentage(cd.getElongation_percentage());
            materailInfoVo.setHardness(cd.getHardness());
            materailInfoVo.setTensile_strength(cd.getTensile_strength());
            //查询该代号材料所有的铸造方法和热处理方式
            List<String> castingMethodListByCode = castingDistinguishService.findCastingMethodListByCode(code);
            materailInfoVo.setCastingMethods(castingMethodListByCode);
            materailInfoVo.setCastingDistinguishs(castingDistinguishService.findHeatTreatmentListByCodeAndCastingMethod(code,castingMethodListByCode.get(0)));
            model.addAttribute("cd",materailInfoVo);
            return "mechanical_info";
        }else{
            //暂无数据
            model.addAttribute("msg","暂无查询数据");
            return "code_query";
        }
    }

    @RequestMapping("/toCodeQueryView")
    public String toCodeQueryView(){
        return "code_query";
    }

    @RequestMapping("/toRangeQueryView")
    public String toRangeQueryView(){
        return "range_query";
    }

    @RequestMapping("/findCastingInformationByName")
    public String findCastingInformationByName(String code,Model model){
        CastingInformation castingInformationByName = castingInformationService.findCastingInformationByName(code);
        model.addAttribute("ci",castingInformationByName);
        return "castingInformation";
    }
}
